<?php

namespace App\Http\Controllers\App;

use App\Enums\AccompanyEnum;
use App\Http\Controllers\Common\TaskController;
use App\Models\GameDB\LotteryDraw\AddPool;
use App\Models\MainDB\Accompany\AccompanyOrder;
use App\Models\MainDB\Welcoms;
use App\Repositories\Activity\CarnivalMatchRepository;
use App\Repositories\Activity\Love520Repository;
use App\Repositories\LogRepository;
use App\Resource\User\SearchResource;
use App\Services\Accompany\AccompanyOrderService;
use App\Services\Draw\DrawFactory;
use App\Services\Draw\DrawManager;
use App\Services\Mq\RabbitmqService;
use App\Services\Tools\JwtService;
use App\Services\Tools\RedisService;
use Illuminate\Support\Facades\DB;
use App\Services\Tools\PushChatService;
use App\Services\Tools\PushService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\MainDB\GetIpInfo;

use App\Jobs\QiPao\GiftJob;


class TestController
{
    public function index(Request $request)
    {
        app('model_gift_backpack')->addGift(6468, 35, 1, 11, 4, 'hhh测试');
        die;
        $ip_info = get_ip_info('123.164.77.102');
        print_r($ip_info);die;
        $data = $request->input('data');
        $value = $request->input('value');
        $key = $request->input('key'); //

        $list = explode($value,$data);
        $a = [];
        foreach ($list as  $k => $v){
            if($v == $key){
                $a[$k] = $v ;
            }
        }
        return [
            'list' => $a,
            'count' => count($a)
        ];

        print_r($ip_info);

        die;
        $repo = new Love520Repository();
        $repo->sendReward();
        echo '发送奖励成功';
        die;
//        $templatePrizes = app('model_gashapon_pool_template')->where('group_id', 20)->get();
        /*$templatePrizes = app('model_gashapon_pool_template')->where('group_id', 16)->get();//公
        $sumCount  = $templatePrizes->sum('count');
        $prizeList = $templatePrizes->toArray();
        if ($sumCount < 1) {
            return false;
        }

        //根据剩余数量进行权重抽取
        $prizeId = 0;
        $groupId = 0;
        //权重总值
        $totalWeight = $sumCount;
        $randomId    = mt_rand(1, $totalWeight);
        $weight      = 0;
        $drawPrizes  = [];
        $p_top = [];
        while ($sumCount > 0) {
            foreach ($prizeList as $k => &$mPool) {
                if ($randomId > $weight && $randomId <= ($mPool['count'] + $weight)) {
                    echo $randomId.'++';die;
                    $prizeId = $mPool['gift_template_id'];
                    $groupId = $mPool['group_id'];
                    $mPool['count']--;
                    $totalWeight--;
                    $weight = 0;
                    break;
                } else {
                    $weight += $mPool['count'];
                    echo $randomId.'--';die;
                }
            }
            if ($totalWeight > 0) {
                $randomId = mt_rand(1, $totalWeight);
            }

            $drawPrizes[] = $groupId . "_" . $prizeId;
            $sumCount--;

            if (in_array($prizeId, [9, 10, 11, 12, 13, 14, 15, 16])) {
                $p_top[] = count($drawPrizes) .'_'.  $prizeId;
            }
        }
        print_r($p_top);die;
        print_r($drawPrizes);die;
        die;*/

        $ip = get_real_ip();
        $ip_info = get_ip_info($ip);
        print_r($ip_info);die;
        die;
        $room_data = app('model_user_room')->get();
        $redis = app('redis');
        foreach ($room_data as $room) {
            $redis_key = 'room:info:'.$room->id;
            $room_data = $redis->hGetAll($redis_key);
            if (!isset($room_data['type_id']) || !isset($room_data['pk_switch'])) {
                print_r($room_data);
            }
        }
        die;
        echo 'aabb';
        die;
        echo JwtService::generate_token(['user_id' => 4214]);
        die;
        (new Welcoms())->getWelcomeNewUser(3582);
    }

    public function gift(Request $request)
    {
        $token = $request->header('token');
        $page = $request->input('page') ?? 1;
//        if ($token != 'qwewewe5wqeqweqwe') {
//            echo 'okk';
//            die;
//        }
        $perPage = 30;
        try {
            $file = base_path() . '/images/';
            $giftlist = app('model_mall_products')->where('id', '<=',395)->paginate($perPage, ['id', 'picture', 'special'], 'page', $page)->toArray();
            $giftlist = $giftlist['data'];
            foreach ($giftlist as $gift) {
                $pir = $gift['picture'];
                $pir_base = basename($pir);
                if ($this->check_pir($pir)) {
                    $data = file_get_contents($pir);
                    file_put_contents($file.$pir_base, $data);
                }
                $pir_small = $gift['special'];
                $pir_small_base = basename($pir_small);
                if ($this->check_pir($pir_small)) {
                    $pir_small_data = file_get_contents($pir_small);
                    file_put_contents($file.$pir_small_base, $pir_small_data);
                }
            }

//                $pir_small = $gift['special'];
//                if ($this->check_pir($pir_small)) {
//                    $data = file_get_contents($pir_small);
//                    $pir_small_base = basename($pir_small);
//                    if (!file_exists($file.$pir_small_base)) {
//                        file_put_contents($file.$pir_small_base, $data);
//                    }
//                }
//            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }

        echo 'okk';
    }

    public function check_pir($url)
    {
        if (@fopen($url, 'r')) {
            return true;
        } else {
            return false;
        }
    }

    public function push_give_gift()
    {

        die;
        $sql = "select a.id,b.id bid,a.total_price,a.room_id,a.type
 from ln_give_gift a left join guild.sc_income b on b.give_id = a.id where b.give_id is null and a.add_time > 1723021803 and a.add_time < 1723036203 and a.room_id <> 0 and a.gift_id not in(910,919, 895) limit 10";

        $data = DB::select($sql);
        $ids = [];
        foreach ($data as $give) {
            $ids[] = $give->id;
        }
        if (!empty($ids)) {
            $job = new GiftJob($ids);
            dispatch($job)->onQueue('giftQueue');
        }
        print_r($ids);
        print_r(implode(',', $ids));
        echo '推送give_gift成功';
    }

    public function set_ip_info($type = 1)
    {
        //  type=2  数据库存redis
        $redis = app('redis')->connection('ktv');
        $redis_key = 'all:ip:info';

        //type=1  redis 存数据库
        if ($type == 1) {
            $insertAll = [];
            $all_ip_info = $redis->hgetall($redis_key);
            foreach ($all_ip_info as $key => $value) {
                $insertAll[] = [
                    'ip' => $key,
                    'ip_info' => $value
                ];
            }
            if (!empty($insertAll)) {
                GetIpInfo::query()->insert($insertAll);
            }
        } else {
            $all_ip_info = GetIpInfo::query()->get()->toArray();
            $all_ip_info = array_column($all_ip_info, 'ip_info', 'ip');
            $redis->hmset($redis_key, $all_ip_info);
        }

    }

}
